function keep = rotateboxsuppress(detections, scores, threshold,round_length)
% BOXSUPPRESS Box non-maxima suprression
%   KEEP = BOXSUPPRESS(BOXES, SCORES, THRESHOLD)

% remove any empty box (xmax < xmin or ymax < ymin)

keep = false(1, size(detections,2)) ;
if isempty(keep)
    return;
end
temp = [detections([3,4],:) - 1 - round_length ;detections([3,4],:) + 1 + round_length ] ;
temp = boxinclusion(temp, temp) <  threshold ;

detections = reshape([detections(5:12,:);detections(5:6,:)],2,5,[]) ;
dist = zeros(size(detections,3),size(detections,3)) ;
area_1 = zeros(size(detections,3),size(detections,3)) ;
area_1(temp) = - inf ;
area_2 = zeros(size(detections,3),1) ;
for i = 1:(size(detections,3)-1)
    temp1 = detections(:,:,i) ;
    area_2(i,1) = polyarea(temp1(1,:), temp1(2,:));
    for j =(i+1):size(detections,3)
        if area_1(i,j) == -inf
            continue;
        end
        temp2 = detections(:, :, j) ;
        [x, y] = polybool('intersection', temp1(1,:), temp1(2,:), temp2(1,:), temp2(2,:));
        area_1(i,j) = polyarea(x, y);
    end
end
area_1 = area_1 + area_1' ;
temp1 = detections(:,:,end) ;
area_2(end,1) = polyarea(temp1(1,:), temp1(2,:));
dist = area_1 ./ repmat(area_2, 1, size(detections,3)) ;

while true
    [score, best] = max(scores) ;
    if score == -inf
      break ; 
    end
    keep(best) = true ;
    temp = dist(best,:);
    remove = temp >= threshold ;
    scores(remove) = -inf ;
    scores(best) = -inf ; % `best` is not in `remove` if threshold > 1
end